package com.haorui.lygyd.authority.provider;

import com.haorui.base.core.utils.BeanUtil;
import com.haorui.lygyd.authority.authentication.MemberAuthenticationToken;
import com.haorui.lygyd.authority.dto.AuthMember;
import com.haorui.lygyd.authority.jwt.JwtTokenUtil;
import com.haorui.lygyd.employeemanager.entity.MemberEntity;
import com.haorui.lygyd.employeemanager.repository.MemberRepository;
import com.haorui.lygyd.employeemanager.service.MemberService;
import com.haorui.lygyd.wxapi.service.WXUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
 * @author 陈刚
 * @version 1.0
 * @project znsm
 * @package com.haorui.znsm.authority.provider
 * @createDate 2018-03-16 14:15
 * @modfiyDate
 * @function
 */
@Service
@Slf4j
public class MemberAuthenticationProvider implements AuthenticationProvider {
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberRepository memberRepository;
    @Autowired
    private WXUserInfoService wxUserInfoService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        MemberAuthenticationToken memberAuthenticationToken = (MemberAuthenticationToken) authentication;
        String username = (String) memberAuthenticationToken.getPrincipal();
        String password = (String) memberAuthenticationToken.getCredentials();
        String openid = memberAuthenticationToken.getOpenId();
        MemberEntity member = memberRepository.findMemberByShouJiHaoMaAndMiMa(username, password);
        if (member != null) {
            if (member.getShiFouSuoDing()) {
                throw new LockedException("会员锁定微信不能登录");
            } else {
                //todo 小程序上线修改
//                if(openid==null){
//                    throw new BadCredentialsException("非法用户登陆！");
//                }
                //绑定用户信息和会员卡号
                wxUserInfoService.wxgetweixinUserByMemberId(openid, member.getId());
                AuthMember authMember = new AuthMember();
                BeanUtil.beanCopy(member, authMember);
                authMember.setOpenId(openid);
                String token = jwtTokenUtil.generateAccountToken(authMember);
                authMember.setToken(token);
                //加入授权明细
                memberAuthenticationToken.setDetails(authMember);
                //设置授权
                SecurityContextHolder.getContext().setAuthentication(memberAuthenticationToken);
                //设置授权通过
                memberAuthenticationToken.setAuthenticated(true);

            }
        } else {
            throw new BadCredentialsException("账号或密码输入错误,请重新输入");
        }

        return memberAuthenticationToken;
    }

    @Override
    public boolean supports(Class<?> authentication) {

        return MemberAuthenticationToken.class.isAssignableFrom(authentication);
    }
}
